<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程编号" prop="courseNo">
        <el-input
          v-model="queryParams.courseNo"
          placeholder="请输入课程编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程分类" prop="courseType">
        <el-select v-model="queryParams.courseType" placeholder="请选择课程分类" clearable>
          <el-option
            v-for="dict in dict.type.sys_course_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="创建者昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="来源" prop="courseSource">
        <el-select v-model="queryParams.courseSource" placeholder="请选择来源" clearable>
          <el-option
            v-for="dict in dict.type.sys_course_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:course:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:course:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="courseId" />
      <el-table-column label="课程编号" align="center" prop="courseNo" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="封面" align="center" prop="courseCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.courseCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="课程分类" align="center" prop="courseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_course_type" :value="scope.row.courseType"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="courseSort" />
      <!-- <el-table-column label="来源" align="center" prop="courseSource" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_course_source" :value="scope.row.courseSource"/>
        </template>
      </el-table-column> -->
      <el-table-column label="发布状态" align="center" prop="releaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_release_status" :value="scope.row.releaseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="nickName" />
      <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" width="180" align="center" class-name="small-padding fixed-swidth">
        <template slot-scope="scope">
<!--          <router-link :to="'/course/period-sub/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['course:period:list']"
            >课时</el-button>
          </router-link>-->
<!--          <router-link :to="'/course/teacher-sub/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['course:teacher:list']"
            >讲师</el-button>
          </router-link>-->
<!--          <router-link :to="'/course/student-sub/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['course:student:list']"
            >学员</el-button>
          </router-link>-->
<!--          <router-link :to="'/course/material-sub/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['course:material:list']"
            >资料</el-button>
          </router-link>-->
          <router-link :to="'/questionBank/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['questionBank:query']"
            >随堂考试题</el-button>
          </router-link>
          <router-link :to="'/review/interflow-sub/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['review:interflow:list']"
            >问答</el-button>
          </router-link>
<!--          <router-link :to="'/review/estimate-sub/index/' + scope.row.courseNo" class="link-type">
              <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              style="padding:5px"
              v-hasPermi="['review:estimate:list']"
            >评价</el-button>
          </router-link>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-swidth">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['evaluate:query']"
              >详细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleVideo(scope.row)"
            v-hasPermi="['course:course:edit']"
          >课程视频</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:course:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程分类" prop="courseType">
          <el-select v-model="form.courseType" placeholder="请选择课程分类">
            <el-option
              v-for="dict in dict.type.sys_course_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布状态" prop="releaseStatus">
          <el-select v-model="form.releaseStatus" placeholder="请选择课程分类">
            <el-option
              v-for="dict in dict.type.sys_release_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="courseSort">
          <el-input-number v-model="form.courseSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="封面" prop="courseCover">
          <image-upload v-model="form.courseCover"/>
        </el-form-item>
        <el-form-item label="简介" prop="courseRemark">
          <el-input v-model="form.courseRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详情" prop="courseInfo">
          <editor v-model="form.courseInfo" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详细start -->
    <el-dialog :title="title" :visible.sync="courseView"  width="900px" append-to-body>
      <el-form ref="viewForm" :model="viewForm" label-width="100px" size="mini">
        <el-form-item label="课程信息："></el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程ID：">
              <span>{{ viewForm.courseId }}</span>
            </el-form-item>
          </el-col>
                    <el-col :span="12">
            <el-form-item label="课程编号：">
              <span>{{ viewForm.courseNo }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程名称：">
              <span>{{ viewForm.courseName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程封面：">
                <image-preview :src="viewForm.courseCover" :width="50" :height="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="简介：">
              <span>{{viewForm.courseRemark}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="详情：">
              <span>{{viewForm.courseInfo}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程分类：">
              <dict-tag
                :options="dict.type.sys_course_type"
                :value="viewForm.courseType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序：">
              <span>{{ viewForm.courseSort }}</span>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
          <el-col :span="12">
            <el-form-item label="发布状态：">
              <dict-tag
                :options="dict.type.sys_release_status"
                :value="viewForm.releaseStatus"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建者：">
              <span>{{ viewForm.nickName }}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="发布日期：">
              <span>{{ parseTime(viewForm.releaseTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建日期：">
              <span>{{
                parseTime(viewForm.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item
          label="试卷信息："
          style="margin-top: 1.5rem"
        ></el-form-item>
        <el-table v-loading="paperloading" :data="paperList">
          <el-table-column label="课程试卷ID" align="center" prop="coursePaperId" />
          <el-table-column label="试卷名称" align="center" prop="paperName">
            <template slot-scope="scope">
                <router-link :to="'/course-papersub/index/' + scope.row.coursePaperId" class="link-type">
                  <span>{{ scope.row.paperName }}</span>
                </router-link>
            </template>
          </el-table-column>
          <el-table-column label="课程编号" align="center" prop="courseNo"/>
          <el-table-column label="课程名称" align="center" prop="courseName">
            <template slot-scope="scope">
              <span>{{scope.row.courseName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="考试时长" align="center" prop="elapsedTime">
            <template slot-scope="scope">
              <span>{{scope.row.elapsedTime }}分钟</span>
            </template>
          </el-table-column>
          <el-table-column label="试卷分类" align="center" prop="paperType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_paper_type" :value="scope.row.paperType"/>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="paperStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.paperStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="考试人数" align="center" prop="studentNum" />
          <el-table-column label="审核状态" align="center" prop="checkStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.paper_check_status" :value="scope.row.checkStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="试题总数" align="center" prop="topicNum" />
          <el-table-column label="备注" align="center" prop="paperRemark" :show-overflow-tooltip="true" />
          <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>

          <!-- <el-table-column
            label="操作"
            align="center"
            width="60"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-row>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleUserView(scope.row.userId)"
                  v-hasPermi="['system:user:query']"
                  >详细</el-button
                >
              </el-row>
            </template>
          </el-table-column> -->
        </el-table>
        <pagination
          v-show="paperTotal > 0"
          :total="paperTotal"
          :page.sync="paperParams.pageNum"
          :limit.sync="paperParams.pageSize"
          @pagination="getPaperList"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="courseView = false">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 详细end -->

    <!--  课程视频  -->
    <el-dialog :title="title" :visible.sync="video.open" width="900px" append-to-body>
      <el-row>
        <el-col span="12">
          <video controls autoplay :src="video.src" :style="{width: '400px'}"/>
        </el-col>
        <el-col  span="12">
          <el-upload
            ref="upload"
            :limit="1"
            accept=".mp4"
            :headers="video.upload.headers"
            :action="video.upload.url + '?updateSupport=' + true"
            :disabled="video.upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <el-button @click.stop type="primary" @click="submitFileForm">确 定</el-button>
          </el-upload>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/course/course";
import { listPaper } from "@/api/course/coursePaper";
import { getToken } from '@/utils/auth'

export default {
  name: "Course",
  dicts: ['sys_normal_disable','sys_course_source','sys_release_status','sys_course_type','paper_check_status','sys_paper_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 详情课程试卷遮罩层
      paperloading:true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 试卷总条数
      paperTotal: 0,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 课程试卷表格数据
      paperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      courseView: false,

      // 试卷记录查询参数
      paperParams: {
        pageNum: 1,
        pageSize: 10,
        courseNo: undefined,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseNo: null,
        nickName: null,
        courseName: null,
        courseType: null,
        courseRemark: null,
        courseCover: null,
        courseInfo: null,
        periodId: null,
        courseSort: null,
        courseSource: null,
        releaseTime: null,
        releaseStatus: null,
      },
      // 表单参数
      form: {},
      //详情表单参数
      viewForm: {},
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        courseType: [
          { required: true, message: "请选择课程分类", trigger: "blur" }
        ],
        courseRemark: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ],
        // courseCover: [
        //   { required: true, message: "请上传封面", trigger: "blur" }
        // ],
        releaseStatus:[
          { required: true, message: "请选择发布状态", trigger: "blur" }
        ],
        courseInfo: [
          { required: true, message: "详情不能为空", trigger: "blur" }
        ],
      },

      video: {
        courseNo: null,
        open: false,
        src: null,
        upload: {
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/course/course/uploadVideo/"
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        courseType: null,
        courseRemark: null,
        courseCover: null,
        courseInfo: null,
        periodId: null,
        courseSort: null,
        courseSource: 0,
        releaseTime: null,
        releaseStatus: 1,
        delFlag: "0",
        userId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
        /** 详细按钮操作 */
    handleView(row) {
      const courseId = row.courseId;
       getCourse(courseId).then(response => {
        this.viewForm = response.data;
        this.courseView = true;
        this.title = "课程详情";
      });

      /** 查询课程试卷列表 */
      this. getPaperList(row);
    },

// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.video.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.video.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.setVideoUrl(this.video.courseNo ,true);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询课程试卷列表 */
    getPaperList(row) {
      this.paperParams.courseNo = row.courseNo;
      listPaper(this.paperParams).then(response => {
        this.paperList = response.rows;
        this.paperTotal = response.total;
        this.paperloading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    handleVideo(row){
      this.video.courseNo = row.courseNo;
      this.setVideoUrl(row.courseNo);
      this.setVideoUpload(row.courseNo);
      this.video.open = true;
    },
    setVideoUrl(courseNo ,reflush){
      let tmp = process.env.VUE_APP_BASE_API + '/course/course/playVideo?courseNo='+ courseNo +'&Authorization=' + getToken();
      if(reflush){
        tmp += '&' + new Date().getTime();
      }
      this.video.src =  tmp ;
    },
    setVideoUpload(courseNo){
      this.video.upload.url = process.env.VUE_APP_BASE_API + "/course/course/uploadVideo/" + courseNo;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除课程编号为"' + courseIds + '"的数据项？').then(function() {
        return delCourse(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
